package com.ayoka.ui.assignment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ayoka.R;
import com.ayoka.adapter.CourseAssignmentGradeOptionAdapter;
import com.ayoka.base.BaseRecyclerViewFragment;
import com.ayoka.bean.CourseAssignmentBean.QuestionBean;
import com.ayoka.bean.CourseAssignmentBean.OptionBean;
import com.ayoka.bean.CourseAssignmentBean.ScoreItemAnswerBean;
import com.ayoka.databinding.FragmentCourseAssignmentRecyclerViewBinding;
import com.ayoka.mvp.CourseAssignmentMVP.CourseAssignmentModel.RequestRelationScoreBean;
import com.ayoka.mvp.CourseAssignmentMVP.CourseAssignmentPresenter;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jcp.baselibrary.popup.CommonPopupWindow;

public class CourseAssignmentGradeFragment extends BaseRecyclerViewFragment<RequestRelationScoreBean, FragmentCourseAssignmentRecyclerViewBinding, CourseAssignmentPresenter> {

    public static CourseAssignmentGradeFragment newInstance(QuestionBean assignmentQuestionBean, String currentChoiceIndex) {
        CourseAssignmentGradeFragment assignmentFragment = new CourseAssignmentGradeFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("AssignmentQuestionBean", assignmentQuestionBean);
        bundle.putString("CurrentChoiceIndex", currentChoiceIndex);
        assignmentFragment.setArguments(bundle);
        return assignmentFragment;
    }

    protected QuestionBean assignmentQuestionBean;
    protected String currentChoiceIndex;

    @Override
    protected void configBundleArguments() {
        assignmentQuestionBean = (QuestionBean) getArguments().getSerializable("AssignmentQuestionBean");
        currentChoiceIndex = getArguments().getString("CurrentChoiceIndex");
    }


    @Override
    public int getContentLayout() {
        return R.layout.fragment_course_assignment_recycler_view;
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        View headerView = View.inflate(getActivity(), R.layout.header_course_assignment, null);
        headerView.findViewById(R.id.ll_choice_description).setVisibility(View.GONE);
        headerView.findViewById(R.id.ll_grade_description).setVisibility(View.VISIBLE);
        headerView.findViewById(R.id.ll_input_description).setVisibility(View.GONE);
        TextView tvIndex = (TextView) headerView.findViewById(R.id.tv_question_index);
        tvIndex.setText(currentChoiceIndex);
        TextView tvTitle = (TextView) headerView.findViewById(R.id.tv_grade_title);
        tvTitle.setText("请给以下关系打分");
        TextView tvDescription = (TextView) headerView.findViewById(R.id.tv_grade_description);
        tvDescription.setText(assignmentQuestionBean.Question);
        adapter.addHeaderView(headerView);

        View footerView = View.inflate(getActivity(), R.layout.footer_course_assignment, null);
        Button btnNextQuestion = (Button) footerView.findViewById(R.id.btn_complete);
        btnNextQuestion.setOnClickListener(this);
        adapter.addFooterView(footerView);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return null;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerView;
    }

    @Override
    protected BaseQuickAdapter<RequestRelationScoreBean, BaseViewHolder> getAdapter() {
        CourseAssignmentGradeOptionAdapter adapter = new CourseAssignmentGradeOptionAdapter();
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_add_grade_item){
                    showAddGradeItemPopup(position);
                } else if (view.getId() == R.id.iv_delete_grade_item){
                    adapter.remove(position);
                }
            }
        });
        return adapter;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected boolean getRefreshEnable() {
        return false;
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
    }

    @Override
    public void initData() {
        for (OptionBean option : assignmentQuestionBean.Options) {
            adapter.addData(new RequestRelationScoreBean(option.Option, 0));
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected CourseAssignmentPresenter getPresenter() {
        return null;
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view.getId() == R.id.btn_complete) {
            for (RequestRelationScoreBean gradeBean : adapter.getData()) {
                if (gradeBean != null) {
                    assignmentQuestionBean.AdmixtureAnswer.ScoreItemAnswer.add(new ScoreItemAnswerBean(gradeBean.ItemName, gradeBean.Score));
                }
            }
            if (assignmentQuestionBean.AdmixtureAnswer.ScoreItemAnswer.isEmpty()){
                showToast("至少添加一项");
                return;
            }
            ((CourseAssignmentAdmixtureFragment)getParentFragment()).showNextQuestion();
        }
    }

    private void showAddGradeItemPopup(final int position) {
        CommonPopupWindow popupWindow = new CommonPopupWindow.Builder(getActivity())
                //设置PopupWindow布局
                .setView(R.layout.popup_add_grade_item)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置PopupWindow里的子View及点击事件
                .setCreateViewCallback(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(final PopupWindow popupWindow, final View view, int layoutResId) {
                        final EditText edtGradeName = (EditText) view.findViewById(R.id.edt_grade_item_name);
                        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (edtGradeName.length() == 0){
                                    showToast("请输入关系名称！");
                                    return;
                                }
                                popupWindow.dismiss();
                                String gradeName = edtGradeName.getText().toString().trim();
                                adapter.addData(position, new RequestRelationScoreBean(gradeName, 0));
                            }
                        });
                    }
                })
                //开始构建
                .create();
        //弹出PopupWindow
        popupWindow.showAtLocation(mDataBinding.getRoot(), Gravity.CENTER, 0, 0);
    }
}
